#ifndef MAINWINDOW_H
#define MAINWINDOW_H

/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool. 

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.
  
*/

#include <string>
#include <utility>

#include "mainoptions.h"
#include "logmanager.h"
#include "rescache.h"
#include "scriptmanager.h"
#include "eventmanager.h"
#include "soundsystem.h"

// Note: includes for interfaces and subsystems: physics and logic have been moved to mainoptions.h

/*The MainWindow is the core of the application. Holds instances of every subsystem and manages the
  main loop.
 */
class CCoreApplication
{

    class CCoreApplicationListener : public CBaseEventListener
    {
    public:
        CCoreApplicationListener(string name, CCoreApplication *parent);
        virtual ~CCoreApplicationListener();

        virtual bool processEvent(EventPtr event);

    private:

        CCoreApplication *mParent;

    };

    EventListenerPtr mEventListener;

    friend class CCoreApplication::CCoreApplicationListener;
    friend class CLevelLoader;

public:

    explicit CCoreApplication(const char *argv0, string listenerName = "MainWindowListener");
    ~CCoreApplication();

    void mainLoop();

private:

    void resumeGame();
    void quitGame();
    void setupWorld();

    // menu-related
    void playButtonClickedSound();
    void playTextAcceptedSound();
    void showMenu();

    //local typedefs visible only to the main window

    /*Using auto_ptr to manage the game subsystems makes it easier to control the startup and shutdown sequences.
      Subsystems often depend on another subsystem for initialization (e.g. all log their status and register their
      event listeners). Care must be taken in the order in which they are created and destoyed. In particular,
      manual control on the destruction sequence is highly desirable.*/
    typedef auto_ptr<CLogManager> LogManagerPtr;
    typedef auto_ptr<CResourceCache> ResCachePtr;
    typedef auto_ptr<CScriptManager> ScriptManagerPtr;
    typedef auto_ptr<CEventManager> EventManagerPtr;
    typedef auto_ptr<CSoundSystem> SoundSystemPtr;
    typedef auto_ptr<CLogic> GameLogicPtr;
    typedef auto_ptr<CRenderWindow> RenderPtr;

    CConfigOptions mConfigOptions;
    sf::Clock mainUpdateTimer;
    sf::Clock graphicsUpdateTimer;
    float sceneBoundingRadius;

    bool isRunning;

    LogManagerPtr logManager; //the application log manager.
    ResCachePtr resourceCache; //the resource cache.
    ScriptManagerPtr scriptManager;
    EventManagerPtr eventManager; //the game event managment system
    SoundSystemPtr soundSystem; //the sound system is a Singleton within the application.
    GameLogicPtr gameLogic; //the gameLogic is a Singleton within the application.
    RenderPtr renderWindow; //the 3d rendering window


};

#endif // MAINWINDOW_H
